package com.gin.kotlin.no_gay_alliance.activity.emoji

import android.content.Context
import android.content.Intent
import com.gin.kotlin.no_gay_alliance.adapter.emoji.GenericEmojiGroupPreviewAdapter
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingActivity
import com.gin.kotlin.no_gay_alliance.base.SimpleItemDecoration
import com.gin.kotlin.no_gay_alliance.databinding.ActivityEmojiGroupPreviewBinding
import com.gin.kotlin.no_gay_alliance.model.emoji.GenericEmojiGroup

class EmojiGroupPreviewActivity : AbstractBindingActivity<ActivityEmojiGroupPreviewBinding>() {
    private lateinit var group: GenericEmojiGroup

    /**
     * onCreate方法的后续操作，如 [AbstractBindingActivity.registerLauncher]， 或 [AbstractBindingActivity.attachTabLayout]
     */
    override fun onCreate() {
        val b = this.b ?: return

        group.data?.also {
            b.recyclerView.adapter = GenericEmojiGroupPreviewAdapter(this, handler!!, it.toMutableList())
            b.recyclerView.addItemDecoration(SimpleItemDecoration())
        }

        group.author?.also { b.tvAuthorName.text = "作者: $it" }
    }

    override fun onResume() {
        super.onResume()
        group.getUniqueName().also { setToolbarTitle(it) }
    }

    /**
     * 解析意图
     * @param intent Intent?
     */
    override fun handleIntent(intent: Intent) {
        this.group = parseArgument(intent, KEY_ARGUMENT, GenericEmojiGroup::class.java) { _ -> null }
    }

    companion object {
        private const val KEY_ARGUMENT = "GROUP"

        fun startActivity(context: Context, group: GenericEmojiGroup) =
            context.startActivity(Intent(context, EmojiGroupPreviewActivity::class.java).also { it.putExtra(KEY_ARGUMENT, group) })

    }
}